/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.profession.Profession;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ProfessionBroker;
import systole.view.messages.ErrorMessages;

/**
 *
 * @author jmj
 */
public class ProfessionBrokerDB extends BrokerDB implements ProfessionBroker {

    /**
     * 
     */
    public ProfessionBrokerDB() {
        super();
    }

    public void insert(Profession profession) throws ExceptionDAO {
        try {
            this.logger.logDebug("save profession ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(profession);
            this.logger.logDebug("save profession successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save profession, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void update(Profession profession) throws ExceptionDAO {
        try {
            this.logger.logDebug("updating profession ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(profession);
            this.logger.logDebug("update profession successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on update profession, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public List<Profession> getAllProfessions() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting all professions");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<Profession> list = currentSession.createQuery("from Profession p order by upper(p.name)").list();
            this.logger.logDebug("getting professions successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all professions, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las profesiones", e.fillInStackTrace());
        }
    }

    @SuppressWarnings("unchecked")
    public boolean existProfession(String name, Integer id) throws ExceptionDAO {
        try {
            this.logger.logDebug("exist profession");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Profession.class).
                    add((id == null) ? Restrictions.eq("name", name)
                    : Restrictions.and(Restrictions.eq("name", name), Restrictions.ne("id", id)));
            List<Profession> list = criteria.list();
            this.logger.logDebug("exist profession successfully");
            return !list.isEmpty();
        } catch (HibernateException e) {
            this.logger.logError("error on exist profession, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo verificar si ya existe la profesión", e.fillInStackTrace());
        }
    }

    public Profession getProfessionByName(String name) throws ExceptionDAO {
        try {
            this.logger.logDebug("get Profession by name");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(Profession.class).
                    add(Restrictions.eq("name", name)).setMaxResults(1);
            return (Profession) criteria.uniqueResult();
        } catch (HibernateException e) {
            this.logger.logError("error on get Profession by name, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la profesión", e.fillInStackTrace());
        }
    }
}
